package com.jubaozan.framework.sentinel.springmvc.init;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientAssignConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager;
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterParamFlowRuleManager;
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
import com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.redis.RedisDataSource;
import com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.property.PropertyListener;
import com.alibaba.csp.sentinel.property.SentinelProperty;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.alibaba.csp.sentinel.util.HostNameUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jubaozan.framework.sentinel.constant.RuleKeyConstant;
import com.jubaozan.framework.sentinel.listener.RedisRulePublisher;
import com.jubaozan.framework.sentinel.springmvc.cluster.entity.ClusterGroupEntity;
import com.jubaozan.framework.sentinel.springmvc.constant.ClusterConstants;
import com.jubaozan.framework.sentinel.springmvc.listener.ServerTransportConfigListener;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/7/17 17:54
 */

@Slf4j
public class ClusterRedisDatasourceInitFunc implements InitFunc {
 private static final String APP_NAME = AppNameUtil.getAppName();

 private final String groupId = "SENTINEL_GROUP";

 private final String configDataId = APP_NAME + "-cluster-client-config";
 private final String clusterMapDataId = APP_NAME + ClusterConstants.CLUSTER_MAP_POSTFIX;
 RedisRulePublisher redisRulePublisher;
 RedisConnectionConfig config;
 RuleKeyConstant ruleKeyConstant;
 @Override
 public void init() throws Exception {

  redisRulePublisher = SpringUtil.getBean(RedisRulePublisher.class);
    ruleKeyConstant = SpringUtil.getBean(RuleKeyConstant.class);
  var builder = RedisConnectionConfig.builder();
  builder
      .withHost(SpringUtil.getProperty("spring.redis.host"))
      .withPort(Integer.parseInt(SpringUtil.getProperty("spring.redis.port")))
      .withPassword(SpringUtil.getProperty("spring.redis.password"));

  if (StringUtil.isNotEmpty("spring.redis.database")) {
   builder.withDatabase(Integer.parseInt(SpringUtil.getProperty("spring.redis.database")));
  }

  if (StringUtil.isNotEmpty(SpringUtil.getProperty("spring.redis.timeout"))) {
   builder.withTimeout(Long.parseLong(SpringUtil.getProperty("spring.redis.timeout")));
  }
    config = builder.build();


  // Register client dynamic rule data source.
  initDynamicRuleProperty();

  // Register token client related data source.
  // Token client common config:
  initClientConfigProperty();
  // Token client assign config (e.g. target token server) retrieved from assign map:
  initClientServerAssignProperty();

  // Register token server related data source.
  // Register dynamic rule data source supplier for token server:
  registerClusterRuleSupplier();
  // Token server transport config extracted from assign map:
  initServerTransportConfigProperty();

  // Init cluster state property for extracting mode from cluster map data source.
  initStateProperty();
 }

 private void initDynamicRuleProperty() {
  // ReadableDataSource<String, List<FlowRule>> ruleSource = new RedisDataSource< List<FlowRule>>(config, groupId,
  //     flowDataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
  // FlowRuleManager.register2Property(ruleSource.getProperty());
  //
  // ReadableDataSource<String, List<ParamFlowRule>> paramRuleSource = new RedisDataSource<List<ParamFlowRule>>(config, groupId,
  //     paramDataId, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
  // ParamFlowRuleManager.register2Property(paramRuleSource.getProperty());
 }

 private void initClientConfigProperty() {

  ReadableDataSource<String, ClusterClientConfig> clientConfigDs = new RedisDataSource<ClusterClientConfig>(config, ruleKeyConstant.getClusterClientConfigKey(),
      configDataId, source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
  clientConfigDs.getProperty().addListener(new PropertyListener<ClusterClientConfig>() {

   @Override
   public void configUpdate(ClusterClientConfig value) {
    redisRulePublisher.pushConfig(value, ruleKeyConstant.getClusterClientConfigKey());
     log.info("update ClusterClientConfig:{}", value);
   }

   @Override
   public void configLoad(ClusterClientConfig value) {
    log.info("configLoad ClusterClientConfig:{}", value);
   }
  });
  ClusterClientConfigManager.registerClientConfigProperty(clientConfigDs.getProperty());
 }

 private void initServerTransportConfigProperty() {
  ReadableDataSource<String, ServerTransportConfig> serverTransportDs = new RedisDataSource<ServerTransportConfig>(config, ruleKeyConstant.getServerTransportConfigKey(),
      ruleKeyConstant.getChannel(), source -> {
   List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
   return Optional.ofNullable(groupList)
       .flatMap(this::extractServerTransportConfig)
       .orElse(null);
  });
  PropertyListener serverTransportListener = SpringUtil.getBean(ServerTransportConfigListener.class);
  serverTransportDs.getProperty().addListener(serverTransportListener);
  ClusterServerConfigManager.registerServerTransportProperty(serverTransportDs.getProperty());
 }

 private void registerClusterRuleSupplier() {
  // Register cluster flow rule property supplier which creates data source by namespace.
  // Flow rule dataId format: ${namespace}-flow-rules
  ClusterFlowRuleManager.setPropertySupplier(namespace -> {
   ReadableDataSource<String, List<FlowRule>> ds = new RedisDataSource<List<FlowRule>>(config, groupId,
       namespace + ClusterConstants.FLOW_POSTFIX, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
   return ds.getProperty();
  });
  // Register cluster parameter flow rule property supplier which creates data source by namespace.
  ClusterParamFlowRuleManager.setPropertySupplier(namespace -> {
   ReadableDataSource<String, List<ParamFlowRule>> ds = new RedisDataSource<List<ParamFlowRule>>(config, groupId,
       namespace + ClusterConstants.PARAM_FLOW_POSTFIX, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {}));
   return ds.getProperty();
  });
 }

 private void initClientServerAssignProperty() {
  // Cluster map format:
  // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
  // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
  ReadableDataSource<String, ClusterClientAssignConfig> clientAssignDs = new RedisDataSource<ClusterClientAssignConfig>(config, groupId,
      clusterMapDataId, source -> {
   List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
   return Optional.ofNullable(groupList)
       .flatMap(this::extractClientAssignment)
       .orElse(null);
  });
  ClusterClientConfigManager.registerServerAssignProperty(clientAssignDs.getProperty());
 }

 private void initStateProperty() {
  // Cluster map format:
  // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
  // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
  ReadableDataSource<String, Integer> clusterModeDs = new RedisDataSource<Integer>(config, groupId,
      clusterMapDataId, source -> {
   List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
   return Optional.ofNullable(groupList)
       .map(this::extractMode)
       .orElse(ClusterStateManager.CLUSTER_NOT_STARTED);
  });
  ClusterStateManager.registerProperty(clusterModeDs.getProperty());
 }

 private int extractMode(List<ClusterGroupEntity> groupList) {
  // If any server group machineId matches current, then it's token server.
  if (groupList.stream().anyMatch(this::machineEqual)) {
   return ClusterStateManager.CLUSTER_SERVER;
  }
  // If current machine belongs to any of the token server group, then it's token client.
  // Otherwise it's unassigned, should be set to NOT_STARTED.
  boolean canBeClient = groupList.stream()
      .flatMap(e -> e.getClientSet().stream())
      .filter(Objects::nonNull)
      .anyMatch(e -> e.equals(getCurrentMachineId()));
  return canBeClient ? ClusterStateManager.CLUSTER_CLIENT : ClusterStateManager.CLUSTER_NOT_STARTED;
 }

 private Optional<ServerTransportConfig> extractServerTransportConfig(List<ClusterGroupEntity> groupList) {
  return groupList.stream()
      .filter(this::machineEqual)
      .findAny()
      .map(e -> new ServerTransportConfig().setPort(e.getPort()).setIdleSeconds(600));
 }

 private Optional<ClusterClientAssignConfig> extractClientAssignment(List<ClusterGroupEntity> groupList) {
  if (groupList.stream().anyMatch(this::machineEqual)) {
   return Optional.empty();
  }
  // Build client assign config from the client set of target server group.
  for (ClusterGroupEntity group : groupList) {
   if (group.getClientSet().contains(getCurrentMachineId())) {
    String ip = group.getIp();
    Integer port = group.getPort();
    return Optional.of(new ClusterClientAssignConfig(ip, port));
   }
  }
  return Optional.empty();
 }

 private boolean machineEqual(/*@Valid*/ ClusterGroupEntity group) {
  return getCurrentMachineId().equals(group.getMachineId());
 }

 private String getCurrentMachineId() {
  // Note: this may not work well for container-based env.
  return HostNameUtil.getIp() + SEPARATOR + TransportConfig.getRuntimePort();
 }

 private static final String SEPARATOR = "@";
}
